<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      check-strictly
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('success')">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTreeData } from '@/utils'
import { assignPrem, getRolesDetail } from '@/api/settings'
export default {
  props: {
    curId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPrem({ id: this.curId, permIds })
      this.$message.success(res.message)
      this.$emit('success')
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        // console.log('getPermissionList', res)
        const detail = await getRolesDetail(this.curId)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        this.list = transDataToTreeData(res.data)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
